<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>{{ $t('navbar.companyName') }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="data"
          :props="defaultProps"
          :default-expand-all="true"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >查看部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门' : '增加部门'"
        :visible.sync="showDialog"
        @close="closeDialog"
      >
        <dept-dialog
          ref="refDialog"
          :pid="curId"
          :is-edit="isEdit"
          :list="list"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { arrayToTree } from '@/utils/index'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      curId: '',
      list: [], // 做表单验证需要的数组,传给子组件
      showDialog: false, // 是否显示弹层
      isEdit: false,
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    // 发请求
    this.loadGetDepartment()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取组织架构信息
    async loadGetDepartment() {
      const { data: res } = await getDepartment()
      res.depts.shift()
      this.list = res.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
      this.data = arrayToTree(res.depts)
      console.log('res', res)
      // this.data = res.
    },
    hAdd(id) {
      // 打开对话框
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      // 查看对话框
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      console.log(this.$refs.refDialog)
      this.$nextTick(() => {
        this.$refs.refDialog.loadDetail() // 点击编辑子部门,让她重新发请求获取数据
      })
    },
    hSuccess() {
      this.showDialog = false
      this.loadGetDepartment()
    },
    // 省略其它...
    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadGetDepartment()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 做删除
          this.doDel(id)
        })
        .catch(() => {})
    },
    hClose() {
      this.showDialog = false
    },
    // 当弹框关闭时
    closeDialog() {
      this.$refs.refDialog.resetForm()
    }
  }
}
</script>
